/*
 * VistaGuias.java
 *
 * Created on 20 de noviembre de 2008, 04:43 PM
 */

package ddta;


import javax.swing.DefaultListSelectionModel;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author  julio
 */
public class VistaGuias extends JDialog {

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.8D187C92-715C-C4DF-481F-9421CD1136A0]
    // </editor-fold> 
    private DDTA ddta;
    private DefaultTableModel tableModel;
    private String codAgencia = null;
    /** Creates new form VistaGuias */
    public VistaGuias(JDialog parent, boolean modal, DDTA ddta, String codAgencia) {
        super(parent, modal);
        initComponents();
        this.ddta = ddta;
        this.codAgencia = codAgencia;
        tblGuia.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
        this.tableModel = (DefaultTableModel) tblGuia.getModel();
        mostrarGuias();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cboSexo = new javax.swing.JComboBox();
        txtNombre = new javax.swing.JTextField();
        txtCodigo = new javax.swing.JTextField();
        btnGrabar = new javax.swing.JButton();
        txtFecNac = new javax.swing.JTextField();
        btnCancelar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblGuia = new javax.swing.JTable();
        btnEliminar = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Guias turistas"));

        jLabel1.setText("Codigo:");

        jLabel2.setText("Nombre:");

        jLabel3.setText("Sexo:");

        jLabel4.setText("Fecha Nac.:");

        cboSexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Femenino", "Masculino" }));

        txtNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNombreActionPerformed(evt);
            }
        });

        txtCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodigoActionPerformed(evt);
            }
        });

        btnGrabar.setText("Guardar guia");
        btnGrabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGrabarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.setEnabled(false);
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 80, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnGrabar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar))
                    .addComponent(txtCodigo, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
                    .addComponent(cboSexo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
                    .addComponent(txtFecNac))
                .addGap(142, 142, 142))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cboSexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtFecNac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGrabar)
                    .addComponent(btnCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Listas guias de turistas"));

        tblGuia.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre", "Sexo", "Fecha Nac."
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblGuia);

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        btnEditar.setText("Editar turista");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 451, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEditar)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEliminar)
                    .addComponent(btnEditar)))
        );

        btnSalir.setText("    Salir    ");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnSalir, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalir)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void txtCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodigoActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtCodigoActionPerformed

private void txtNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNombreActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtNombreActionPerformed

private void btnGrabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGrabarActionPerformed
// TODO add your handling code here:
    if (evt.getActionCommand().equals("EDITAR")) {
        actualizarDatos();
    } else {
        registrarDatos();
    }
    btnGrabar.setActionCommand("");
    btnCancelar.setEnabled(false);
}//GEN-LAST:event_btnGrabarActionPerformed

private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
// TODO add your handling code here:
    int iRow = tblGuia.getSelectedRow();
    if (iRow >= 0) {
        String codigo = this.tableModel.getValueAt(iRow, 0).toString();
        this.ddta.getCtlGuias().eliminarGuia(codigo);
        mostrarGuias();
    } else {
        JOptionPane.showMessageDialog(this, "Debe seleccionar una fila", this.getTitle(), JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_btnEliminarActionPerformed

private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
// TODO add your handling code here:
    this.setVisible(false);
    if (this.getParent() != null) {
        if (this.getParent() instanceof VistaReservaGuias) {
            ((VistaReservaGuias)this.getParent()).actualizarGrillas();
        }
    }
}//GEN-LAST:event_btnSalirActionPerformed

private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
// TODO add your handling code here:
    int iSel = tblGuia.getSelectedRow();
    if (iSel >= 0) {
        String codGuia = this.tableModel.getValueAt(iSel, 0).toString();
        mostrarDatosGuia(codGuia);
        btnGrabar.setActionCommand("EDITAR");
        btnCancelar.setEnabled(true);
    } else {
        JOptionPane.showMessageDialog(rootPane, "Seleccione un guia");
    }
}//GEN-LAST:event_btnEditarActionPerformed

private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
// TODO add your handling code here:
    limpiarDatos();
    btnGrabar.setActionCommand("");
    btnCancelar.setEnabled(false);
}//GEN-LAST:event_btnCancelarActionPerformed

private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
// TODO add your handling code here:
     if (this.getParent() != null) {
        if (this.getParent() instanceof VistaReservaGuias) {
            ((VistaReservaGuias)this.getParent()).actualizarGrillas();
        }
    }
}//GEN-LAST:event_formWindowClosed
   private void mostrarGuias(){
       int iRows = this.ddta.getCtlGuias().getGuiasTuristas().size();
       this.tableModel.setNumRows(iRows);
        if (iRows > 0) {
        int i = 0;
        for (GuiaTurista guia : this.ddta.getCtlGuias().getGuiasTuristas()) {
            this.tableModel.setValueAt(guia.getCodigo(), i, 0);
            this.tableModel.setValueAt(guia.getNombre(), i, 1);
            this.tableModel.setValueAt(guia.getSexo(), i, 2);
            this.tableModel.setValueAt(guia.getFechaNacimiento().getFullDate(), i, 3);
            i++;
        }
    }
   
   }
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.A9523A16-5E9C-DC63-C056-2E1678DA9821]
    // </editor-fold> 
    public DDTA getDDTA () {
        return ddta;
    }  

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.D907E3F9-098C-158E-687F-9B10097387D4]
    // </editor-fold> 
    public void setDDTA (DDTA val) {
        this.ddta = val;
    }
    private void registrarDatos() {
        String codigo = this.txtCodigo.getText();
        String nombre = this.txtNombre.getText();
        String fecnac = this.txtFecNac.getText();
        String sexo= (String)this.cboSexo.getSelectedItem();
        Agencia agencia = this.ddta.getCtlAgencias().buscarAgencia(this.codAgencia);
         if (! codigo.equals("") && ! nombre.equals("") && ! fecnac.equals("")){
            if (!Fecha.isValidDateStr(fecnac)) {
                JOptionPane.showMessageDialog(rootPane, "Fecha inválida");
                return;
            }
            if (ddta.getCtlGuias().buscarGuia(codigo) == null){
                ddta.getCtlGuias().registrarGuia( codigo, nombre, fecnac, sexo, agencia);
                mostrarGuias();
                limpiarDatos();
            } else {
                JOptionPane.showMessageDialog(rootPane, "Código ya existe");
           }
        } else {
            JOptionPane.showMessageDialog(rootPane, "Ingrese los datos");
        }
    }
    private void actualizarDatos() {
        String codigo = this.txtCodigo.getText();
        String nombre = this.txtNombre.getText();
        String fecnac = this.txtFecNac.getText();
        String sexo= (String)this.cboSexo.getSelectedItem();
        Agencia agencia = this.ddta.getCtlAgencias().buscarAgencia(this.codAgencia);
         if (! codigo.equals("") && ! nombre.equals("") && ! fecnac.equals("")){
            if (!Fecha.isValidDateStr(fecnac)) {
                JOptionPane.showMessageDialog(rootPane, "Fecha inválida");
                return;
            }
            if (ddta.getCtlGuias().buscarGuia(codigo) != null){
                 ddta.getCtlGuias().actualizarGuia(codigo, nombre, fecnac, sexo, agencia);
                 mostrarGuias();
                limpiarDatos();
            } else {
                JOptionPane.showMessageDialog(rootPane, "Código no existe");
           }
        } else {
            JOptionPane.showMessageDialog(rootPane, "Ingrese los datos");
        }
    }
    private void mostrarDatosGuia(String codGuia) {
        GuiaTurista guia = this.ddta.getCtlGuias().buscarGuia(codGuia);
        if (guia != null) {
            txtCodigo.setText(guia.getCodigo());
            txtNombre.setText(guia.getNombre());
            txtFecNac.setText(guia.getFechaNacimiento().getFullDate());
            cboSexo.setSelectedItem(guia.getSexo());
        }
    }
    private void limpiarDatos() {
     this.txtCodigo.setText("");
     this.txtNombre.setText("");
     this.txtFecNac.setText("");
     cboSexo.setSelectedIndex(-1);
     this.txtCodigo.requestFocusInWindow();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnGrabar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cboSexo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblGuia;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtFecNac;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables

}
